/* eslint-disable no-template-curly-in-string */
import { approvalType, userType } from '../../types';
import RadioControlService from '../radio/service';

class ApprovalTypeService extends RadioControlService {

  onChange(value: string) {
    this.value = value;
    this.updateProperties(value);
  }

  updateProperties(value: string, type?: string) {
    // 无 则 删除bpmn2:multiInstanceLoopCharacteristics flowable:assignee

    // 会签
    // - 在userTask标签添加属性，固定不变：`flowable:assignee="${assignee}" `
    // <bpmn2:multiInstanceLoopCharacteristics flowable:collection="${multiInstanceHandler.getUserIds(execution)}" flowable:elementVariable="assignee">
    //     <!--completionCondition表示通过条件，按照我们原型的设计，会签需使用下面的表达式，可以固定不变-->
    //     <bpmn2:completionCondition xsi:type="bpmn2:tFormalExpression">${nrOfCompletedInstances == nrOfInstances}</bpmn2:completionCondition>
    //   </bpmn2:multiInstanceLoopCharacteristics>

    // 或签
    // <bpmn2:multiInstanceLoopCharacteristics flowable:collection="${multiInstanceHandler.getUserIds(execution)}" flowable:elementVariable="assignee">
    //   <!--或签需使用下面的表达式，可以固定不变-->
    //   <bpmn2:completionCondition xsi:type="bpmn2:tFormalExpression">${nrOfCompletedInstances == 1}</bpmn2:completionCondition>
    // </bpmn2:multiInstanceLoopCharacteristics>

    const element = this.panel.getElement();
    const moddle = this.root.getModdle();

    const buildLoopCharacteristics = (completionCondition: any) => {
      return moddle.create(
        `bpmn:MultiInstanceLoopCharacteristics`,
        {
          'flowable:collection': '${multiInstanceHandler.getUserIds(execution)}',
          'flowable:elementVariable': 'assignee',
          completionCondition
        },
      );
    };

    const getUpdateDatas = {
      [approvalType.none]: () => {
        const updateData: Record<string, any> = {
          'flowable:assignee': undefined,
          loopCharacteristics: undefined
        };
        // 发起人类型不要改变 assignee
        if (type === userType.initiator) {
          delete updateData['flowable:assignee'];
          updateData['flowable:assignee'] = '${initiator}';
        }
        return updateData;
      },
      [approvalType.and]: () => {
        const completionCondition = moddle.create(
          'bpmn:FormalExpression',
          { body: '${nrOfCompletedInstances == nrOfInstances}' },
        );

        const loopCharacteristics = buildLoopCharacteristics(completionCondition);
        return {
          'flowable:assignee': '${assignee}',
          loopCharacteristics
        };
      },
      [approvalType.or]: () => {
        const completionCondition = moddle.create(
          'bpmn:FormalExpression',
          { body: '${nrOfCompletedInstances == 1}' },
        );

        const loopCharacteristics = buildLoopCharacteristics(completionCondition);
        return {
          'flowable:assignee': '${assignee}',
          loopCharacteristics
        };
      }
    };
    if (!getUpdateDatas[value]) {
      throw new Error('未知的审批类型');
    } else {
      const properties = getUpdateDatas[value] ? getUpdateDatas[value]() : getUpdateDatas;
      this.root.getModeling().updateProperties(element, properties);
    }
  }

  init() {
    const element = this.panel.getElement();

    const loopCharacteristics = element.businessObject.loopCharacteristics;
    if (loopCharacteristics) {
      const completionCondition = loopCharacteristics.completionCondition;
      if (completionCondition) {
        const body = completionCondition.body;
        if (body === '${nrOfCompletedInstances == nrOfInstances}') {
          this.value = approvalType.and;
        } else if (body === '${nrOfCompletedInstances == 1}') {
          this.value = approvalType.or;
        } else {
          this.value = approvalType.none;
        }
      } else {
        this.value = approvalType.none;
      }
    } else {
      this.value = approvalType.none;
    }
  }

}

export default ApprovalTypeService;
